package com.ty.user.starter.util;


import com.ty.user.starter.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.subject.Subject;

/**
 * shiro工具类
 */
@Slf4j
public class ShiroUtils {
    private ShiroUtils() {
    }

    /**
     * 获取登录用户信息
     *
     * @return 用户信息
     */
    public static User getUser() {
        try {
            Subject subject = SecurityUtils.getSubject();
            if (null != subject) {
                return (User) subject.getPrincipal();
            }
        } catch (UnavailableSecurityManagerException e) {
            log.error("当前登录未走shiro, 获取用户信息失败");
        }
        return null;
    }
}
